<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="ClickAll" />
    </label>
    <span>
      <span>已完成{{ finished }}</span> / 全部{{ todos.length }}
    </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>

<script lang="ts">
import { Todo } from '../types/todo'
import { defineComponent, computed } from 'vue'

export default defineComponent({
  name: 'Footer',
  props: {
    todos: {
      type: Array as () => Todo[],
      default: () => []
    }
  },
  emits: ['update:todos'],
  setup(props, { emit }) {
    const finished = computed(() => {
      return props.todos.filter(item => item.isCompleted === true).length
    })
    const ClickAll = computed({
      get() {
        return !props.todos.find(item => !item.isCompleted)
      },
      set(value: boolean) {
        props.todos.forEach(item => (item.isCompleted = value))
      }
    })
    const clearAll = () => {
      const clearTodos = props.todos.filter(item => !item.isCompleted)
      emit('update:todos', clearTodos)
    }
    return {
      finished,
      ClickAll,
      clearAll
    }
  }
})
</script>

<style scoped lang="scss">
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>